<?php

/**
 * accountCustomer actions.
 *
 * @package    erp-crm
 * @subpackage accountCustomer
 * @author     Milwell H. Sia
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 */
class accountCustomerActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  
  
  public function executeIndex(sfWebRequest $request){
    $contact_iden = $request->getParameter('iden');
    $contact = ContactPeer::retrieveByPk($contact_iden);
    $this->forward404Unless($contact);
    $accountCustomer = $contact->getAccountCustomer();
    if(! $accountCustomer)
      $this->forward('accountCustomer','create');
    else
      $this->forward('accountCustomer','edit');
  }
  
  
  public function executeCreate(sfWebRequest $request)
  {
    $contact_iden = $request->getParameter('iden');
    $this->contact = ContactPeer::retrieveByPk($contact_iden);
    $this->forward404Unless($this->contact);
    $this->accountCustomerForm = new AccountCustomerForm();
    $this->accountCustomerForm->setDefault('contact_iden',$contact_iden);
    $this->setLayout(false);
  }
  
  public function executeEdit(sfWebRequest $request)
  {
    $contact_iden = $request->getParameter('iden');
    $this->contact = ContactPeer::retrieveByPk($contact_iden);
    $this->forward404Unless($this->contact);
    $accountCustomer = $this->contact->getAccountCustomer();
    $this->accountCustomerForm = new AccountCustomerForm($accountCustomer);
    $this->setTemplate('create');
    $this->setLayout(false);
  }
  
  public function executeInsert(sfWebRequest $request){
    $account_customer_params = $request->getParameter('account_customer');
    $contact_iden = $account_customer_params['contact_iden'];
    $this->contact = ContactPeer::retrieveByPk($contact_iden);
    $this->forward404Unless($this->contact);
    $accountCustomerParams = $account_customer_params;
    $accountCustomerParams['contact_iden'] = $contact_iden;
    $this->accountCustomerForm = new AccountCustomerForm();
    $this->accountCustomerForm->bind($accountCustomerParams);
    if($this->accountCustomerForm->isValid()){
      $this->accountCustomerForm->save();
      $this->redirect('accountCustomer/edit?iden='.$this->accountCustomerForm->getObject()->getContactIden());
    }
    $this->setTemplate('create');
  }
  
  public function executeUpdate(sfWebRequest $request){
    $account_customer_params = $request->getParameter('account_customer');
    $contact_iden = $account_customer_params['contact_iden'];
    $this->contact = ContactPeer::retrieveByPk($contact_iden);
    $this->forward404Unless($this->contact);
    $accountCustomer = $this->contact->getAccountCustomer();
    $this->forward404Unless($accountCustomer);
    $this->accountCustomerForm = new AccountCustomerForm($accountCustomer);
    $this->accountCustomerForm->bind($account_customer_params);
    if($this->accountCustomerForm->isValid()){
      $this->accountCustomerForm->save();
      $this->redirect('accountCustomer/edit?iden='.$accountCustomer->getContactIden());
    }
    $this->setTemplate('create');
  }
  
  public function executeDelete(sfWebRequest $request){
    $contact_iden = $request->getParameter('iden');
    $accountCustomer = AccountCustomerPeer::retrieveByPk($contact_iden);
    $this->forward404Unless($accountCustomer);
    try{
      $accountCustomer->delete();
      $this->redirect('accountCustomer/create?iden='.$contact_iden);
    }catch (Exception $e){
      $request->getParameterHolder()->set('message',$e->getMessage());
      $this->forward('main','error');
    }
  }
  
  public function executeGetBalance(sfWebRequest $request){
    $customer_iden = $request->getParameter('contact_iden');
    $con = Propel::getConnection();
    $total_balance = myQuery::getCustomerTotalBalance($con,$customer_iden);
    return $this->renderText(json_encode(array('balance'=>$total_balance,)));
  }
  
  
}
